from pptx import Presentation


def read_pptx_content(file_path):
    """
    读取PPTX文件内容并结构化输出
    :param file_path: PPTX文件路径
    :return: 包含幻灯片内容的字典列表
    """
    prs = Presentation(file_path)
    slides_content = []

    for slide_idx, slide in enumerate(prs.slides, 1):
        slide_data = {
            "slide_number": slide_idx,
            "shapes": [],
            "notes": slide.notes_slide.notes_text_frame.text if slide.has_notes_slide else ""
        }

        for shape in slide.shapes:
            shape_data = {
                "shape_type": str(shape.shape_type),
                "text": "",
                "image": None
            }

            if shape.has_text_frame:
                text_content = []
                for paragraph in shape.text_frame.paragraphs:
                    text_content.append(paragraph.text)
                shape_data["text"] = "\n".join(text_content)

            elif shape.shape_type == 13:  # 图片类型
                shape_data["image"] = {
                    "width": shape.width,
                    "height": shape.height,
                    "image_format": shape.image.ext
                }

            slide_data["shapes"].append(shape_data)

        slides_content.append(slide_data)

    return slides_content


if __name__ == "__main__":
    pptx_file = r"D:\工作\IT公司RAG打榜比赛营销相关数据需求\china_mobile_docs\宁夏\全球通产品说明书-下发版250303.pptx"  # 替换为实际文件路径
    content = read_pptx_content(pptx_file)

    # 打印读取结果
    for slide in content:
        print(f"\n=== 幻灯片 {slide['slide_number']} ===")
        print(f"备注: {slide['notes']}")

        for shape in slide["shapes"]:
            if shape["text"]:
                print(f"\n文本内容 ({shape['shape_type']}):")
                print(shape["text"])
            elif shape["image"]:
                print(f"\n图片信息: {shape['image']}")
            else:
                print(shape)